package com.taorenqi.business.fragment.mine.more;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.taorenqi.business.R;
import com.taorenqi.business.activity.LoginActivity;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.event.RechargeEvent;
import com.taorenqi.business.event.StartFragmentEvent;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.fragment.login.LoginFragment;
import com.taorenqi.business.utils.Encoder;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;
import com.taorenqi.business.widget.subscribers.PageDataSubscriberOnNext;
import com.taorenqi.business.widget.subscribers.ProgressSubscriber;
import com.taorenqi.data.cache.ACache;
import com.taorenqi.data.cache.EnumKey;
import com.taorenqi.data.common.BasePageableResponse;
import com.taorenqi.data.entity.login.CodeDataEntity;
import com.taorenqi.data.service.LoginDataHttpRequest;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 * A simple {@link Fragment} subclass.
 */
public class ModifyLoginPwdFragment extends BaseBackFragment {


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.modify_old_pwd)
    EditText modifyOldPwd;
    @Bind(R.id.modify_new_pwd)
    EditText modifyNewPwd;
    @Bind(R.id.modify_new_pwd_check)
    EditText modifyNewPwdCheck;
    @Bind(R.id.more_sumit)
    TextView moreQuit;

    private BasePageableResponse<CodeDataEntity> data;
    private PageDataSubscriberOnNext<BasePageableResponse<CodeDataEntity>> subscriber;

    public static ModifyLoginPwdFragment newInstance() {
        // Required empty public constructor
        ModifyLoginPwdFragment fragment = new ModifyLoginPwdFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_modify_login_pwd, container, false);
        ButterKnife.bind(this, view);
        initToolbar();
        initHttpRequestData();
        return view;
    }

    private void initHttpRequestData() {
        subscriber = new PageDataSubscriberOnNext<BasePageableResponse<CodeDataEntity>>() {
            @Override
            public void onNext(BasePageableResponse<CodeDataEntity> entity) {
                data = entity;
                ToastUtil.showToast(_mActivity,"修改密码成功");
                Intent intent = new Intent();
                intent.setClass(_mActivity, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                EventBus.getDefault().post(new RechargeEvent());
            }
        };
    }

    private void initToolbar() {
        toolbar.setTitle("修改密码");
        initToolbarNav(toolbar);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.more_sumit)
    public void onViewClicked() {
        String oldPassword = ACache.get(_mActivity).getAsString(EnumKey.User.LOGIN_PWD);
        String password = modifyNewPwd.getText().toString().trim();
        String passwordAgain = modifyNewPwdCheck.getText().toString().trim();
        String oldInput = modifyOldPwd.getText().toString().trim();

        Map<String,String> map = new HashMap<>();
        map.put("password",oldInput);
        map.put("newPassword",password);
        String sign = Encoder.getSigns(map);
        if (TextUtils.isEmpty(oldInput) || !oldInput.equals(oldPassword)){
            ToastUtil.showToast(_mActivity,"原密码输入不正确，请重新输入");
            return;
        }

        if (TextUtils.isEmpty(password) || password.length() < 6) {
            ToastUtil.showToast(_mActivity, "请按要求输入密码");
            return;
        }

        if (TextUtils.isEmpty(passwordAgain) || passwordAgain.length() < 6) {
            ToastUtil.showToast(_mActivity, "请按要求输入密码");
            return;
        }
        if (password.equals(oldInput)){
            ToastUtil.showToast(_mActivity,"新密码和原密码一致，请重新输入");
            return;
        }
        if (!password.equals(passwordAgain)) {
            ToastUtil.showToast(_mActivity, "两次密码不一致");
            return;
        }
        LoginDataHttpRequest.getInstance(MyApp.getApp()).postModifyPwd(new ProgressSubscriber(subscriber,_mActivity),oldInput,password,sign);
    }
}
